package com.wry.myphotowall;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.wry.myphotowall.activity.PhotoPickerActivity;
import com.wry.myphotowall.adapter.PhotoWallAdapter;
import com.wry.myphotowall.util.Common;
import com.wry.myphotowall.util.ToastUtil;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    TextView tv_Start;
    RadioGroup mRadioGroup1, mRadioGroup2, mRadioGroup3;
    EditText et_MaxNum;
    LinearLayout mLayout_Cut, mLayout_Camera;

    private int chooseMode = 0;//选择模式 0：单选  1：多选
    private int maxNum = 9;//多选时候最多选择张数
    private boolean isNeedCut = true;//是否需要裁剪图片
    private boolean isShowCamers = true;//是否需要显示拍照

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioGroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
        mRadioGroup2 = (RadioGroup) findViewById(R.id.radiogroup2);
        mRadioGroup3 = (RadioGroup) findViewById(R.id.radiogroup3);
        tv_Start = (TextView) findViewById(R.id.tv_start);
        et_MaxNum = (EditText) findViewById(R.id.et_maxnum);
        mLayout_Cut = (LinearLayout) findViewById(R.id.layout_cut);
        mLayout_Camera = (LinearLayout) findViewById(R.id.layout_camera);
        tv_Start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoWallAdapter.mSelectedImage.clear();
                if (chooseMode == 0) {
                    maxNum = 1;
                    PhotoPickerActivity.actionStart(MainActivity.this, maxNum, null, isShowCamers, isNeedCut);
                } else {
                    isNeedCut = false;
                    if(Common.empty(et_MaxNum.getText().toString())){
                        maxNum = 9;
                    }else {
                        maxNum = Integer.parseInt(et_MaxNum.getText().toString());
                    }
                    PhotoPickerActivity.actionStart(MainActivity.this, maxNum, null, isShowCamers, isNeedCut);
                }
            }
        });
        mRadioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.radio1) {
                    chooseMode = 0;
                    mLayout_Cut.setVisibility(View.VISIBLE);
                    et_MaxNum.setVisibility(View.GONE);
                } else if (checkedId == R.id.radio2) {
                    chooseMode = 1;
                    mLayout_Cut.setVisibility(View.GONE);
                    et_MaxNum.setVisibility(View.VISIBLE);
                }
            }
        });
        mRadioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.radio3) {
                    isNeedCut = true;
                } else if (checkedId == R.id.radio4) {
                    isNeedCut = false;
                }
            }
        });
        mRadioGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.radio5) {
                    isShowCamers = true;
                } else if (checkedId == R.id.radio6) {
                    isShowCamers = false;
                }
            }
        });
        et_MaxNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!Common.empty(s)) {
                    maxNum = Integer.parseInt(s.toString());
                } else {
                    maxNum = 9;
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PhotoPickerActivity.REQUEST_CODE://相机回调 //选择好的图片列表回调  //剪切图片回调
                    if (data != null) {
                        ArrayList<String> photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                        ToastUtil.showShort(photos.get(0));
                        PhotoWallAdapter.mSelectedImage.clear();
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
